<?php
/**
 * ProductItem Dao definition 
 *
 * @author guzhi
 * @since 2009-03-25
 */
 
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class ProductItemDao extends BaseDao{

    protected $_name;

    protected $products;
    
    public function  _setup() {
        $this->_name = $this->prefix() . "product_items";
        $this->products = $this->prefix() . "products";
        parent::_setup();
    }
    
    public function addItem($item){
    	return $this->insertData($item);
    }
    
    public function updateItem($id, $item){
    	$where =  $this->db_w->quoteInto('id = ?', $id);
    	$this->updateData($item, $where);
    }
    
    public function deleteItem($id){
    	$isDel = array("is_delete"=>1);
    	$where = $this->db_w->quoteInto('id = ?', $id);
    	$this->updateData($isDel, $where);
    }

    public function deleteItemByProductId($id){
    	$isDel = array("is_delete"=>1);
    	$where = $this->db_w->quoteInto('product_id = ?', $id);
    	$this->updateData($isDel, $where);
    }
    
    public function fetchItemsByPid($productId, $isDel){
    	$select = $this->db_r->select();
    	$select->from($this->_name, "*");
    	$select->where('product_id=?', $productId);
    	$select->where('is_delete=?', $isDel);
    	$select->order('id');
    	$sql = $select->__toString();
    	$result = $this->db_r->fetchAll($sql);
    	return $result;
    }
    
    public function getSprice($id) {
        $select = $this->db_r->select();
        $select->from($this->_name,"sale_price");
        $select->where('id=?', $id);
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchOne($sql);
        return $result;
    }

    public function getListPriceByIds($ids){
        $select = $this->db_r->select();
        $select->from($this->_name,array("id","list_price"));
        $select->where("id in ($ids)");
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $results = $this->db_r->fetchAll($sql);
        $listPrice = array();
        foreach($results as $result){
            $listPrice[$result['id']] = $result['list_price'];
        }
        return $listPrice;
    }

    public function getProductItemById($id){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->join($this->products,$this->_name.".product_id=".$this->products.".id",array("title"));
        $select->where($this->_name.".id = ?",$id);
        $select->where($this->_name.".is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }

    public function getProductItemByIds($ids){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->join($this->products,$this->_name.".product_id=".$this->products.".id",array("title"));
        $select->where($this->_name.".id in ($ids)");
        $select->where($this->_name.".is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    //更新默认图片信息
    public function updateDefaultImageByItenId($itemId, $id){
    	$imageStatus = array("default_item_image_id" => $id);
        $where = $this->db_w->quoteInto('id = ?', $itemId);
        $this->updateData($imageStatus, $where);
    }
    
}